//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.ky;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.KyCardDao;
import com.aidong.model.wills.KyCard;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"kyCard"})
public class KyCardController extends BaseController {
    @Resource
    KyCardDao kyCardDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;

    public KyCardController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("ky/card/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "noFrom",required = false) String noFrom, @RequestParam(value = "noTo",required = false) String noTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 50);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("status", status);
        cd.ge("cardCode", noFrom);
        cd.le("cardCode", noTo);
        this.kyCardDao.queryPageList(pg, cd, (new OrderSet()).asc("cardCode"));
        ModelAndView mv = new ModelAndView("ky/card/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("ky/card/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@RequestParam("shopCode") String shopCode, @RequestParam("no1") String no1, @RequestParam("no2") String no2) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请选择收卡门店");
        } else if(!this.chkNull(no1) && !this.chkNull(no2)) {
            if(no1.length() != no2.length()) {
                return this.getResult(false, "开始和结束卡号长度必须一致");
            } else if(this.kyCardDao.chkCardExists(no1, no2)) {
                return this.getResult(false, "卡号已经存在");
            } else {
                int nn = no1.length();

                int n;
                for(n = 0; n < nn - 1 && no1.charAt(n) == no2.charAt(n); ++n) {
                    ;
                }

                Integer n1 = this.parseInt(no1.substring(n));
                Integer n2 = this.parseInt(no2.substring(n));
                if(n1 != null && n2 != null) {
                    if(n2.intValue() - n1.intValue() > 10000) {
                        return this.getResult(false, "一次生成的卡太多");
                    } else {
                        String st = no1.substring(0, n);
                        n = nn - n;
                        ArrayList cardList = new ArrayList();

                        for(int i = n1.intValue(); i <= n2.intValue(); ++i) {
                            String t = "00000000" + i;
                            cardList.add(st + t.substring(t.length() - n));
                        }

                        try {
                            this.kyCardDao.addCardList(cardList, shopCode, this.getLoginUserCode());
                            this.addUserLog("K-Yoga配卡", "门店:" + this.cache.getValue("shopName", shopCode) + ",卡号:" + no1 + "-" + no2);
                        } catch (Exception var13) {
                            this.log.error("add card", var13);
                            return this.getResult(false, "保存发生错误");
                        }

                        return this.getSuccess(String.valueOf(cardList.size()), "生成了" + cardList.size() + "张卡");
                    }
                } else {
                    return this.getResult(false, "序号部分必须是数字");
                }
            }
        } else {
            return this.getResult(false, "请输入开始卡号和结束卡号");
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") String ids) {
        String[] ss = ids.split(",");
        ArrayList list = new ArrayList();
        String[] e = ss;
        int len$ = ss.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String c = e[i$];
            KyCard card = this.kyCardDao.getByCardCode(c);
            if(card == null) {
                return this.getResult(false, "没有这个卡号:" + c);
            }

            if(card.getStatus().intValue() != 1) {
                return this.getResult(false, "已使用,不能删除:" + c);
            }

            list.add(card.getCardCode());
        }

        if(list.size() == 0) {
            return this.getResult(false, "请选择卡号");
        } else {
            try {
                this.kyCardDao.delCardList(list);
                this.addUserLog("K-Yoga删除配卡", list.size() + "张卡");
            } catch (Exception var9) {
                this.log.error("del card", var9);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
